<?php

namespace App\Http\Controllers\AdminApi\User;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StorePermissionsRequest;
use App\Http\Requests\Admin\UpdatePermissionsRequest;
use App\Http\Models\Admin\Permission_;
use App\Http\Models\Admin\ClassifyPermissions;
use App\Http\Models\Admin\PermissionsDetails;
use Spatie\Permission\Guard;
use Illuminate\Support\Facades\Validator;

// use Validator;

class PermissionsController extends Controller
{
    /**
     * Display a listing of Permission.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // if (!Gate::allows('watch power')) {
        //     return abort(401);
        // } 
        $permission = new Permission_();
        $permissions = $permission->with(['hasDetails', 'classify_belongsToMany'])->paginate(20);
        return response()->json(['code' => 20000, 'message' => '查询权限成功', 'data' => compact('permissions')]);
    }

    /**
     * Show the form for creating new Permission.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
    }

    /**
     * Store a newly created Permission in storage.
     *
     * @param  \App\Http\Requests\StorePermissionsRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function insertPermissions(Request $request)
    {
        // if (!Gate::allows('create power')) {
        //     return abort(401);
        // }
        // $this->validator($request->all())->validate();
        $name = $request->get('name');
        $instructions = $request->get('instructions');
        $id = $request->get('id');

        $permission = Permission_::where(array('name' => $name))->get();

        //如果此权限已经存在
        if (!empty(count($permission))) {
            return response()->json(['code' => 20001, 'message' => '此权限已存在', 'data' => $permission]);
        }
        $guard_name =  Guard::getDefaultName(static::class);
        $arr = [
            'name' => $name,
            // 'guard_name' => $guard_name
        ];
        // var_dump($arr);die;
        $inser_per = Permission_::create($arr);
        // var_dump($inser_per);die;
        $inser_per->classify_belongsToMany()->sync($request->input('id'));

        //操作关联数据 (没有找到关联存储比较好的方法 这里使用笨方法)
        $details = new PermissionsDetails;
        $details->permission_id = $inser_per->id;

        $permission = $details->where(array('permission_id' => $inser_per->id))->get();
        $details->explain = $request->input('instructions');

        if (empty(count($permission))) {
            $details->save();
        } else {
            $details->update();
        }
        // echo response()->json($inser_per); die;

        return response()->json(['code' => 20000, 'message' => '权限添加成功', 'data' => $details]);
    }


    /**
     * Show the form for editing Permission.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
    }

    /**
     * Update Permission in storage.
     *
     * @param  \App\Http\Requests\UpdatePermissionsRequest  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function updatePer(Request $request)
    {
        // if (!Gate::allows('update power')) {
        //     return abort(401);
        // }
        // echo 1;die;
        $name = $request->get('name'); //权限的名称
        $instructions = $request->get('instructions'); //权限的说明
        $title = $request->get('title'); //角色名称
        $id = $request->get('id'); //角色id
        $perId = $request->get('perId');  //权限id
        $arr = [
            'name' => $name
        ];
        // echo response()->json($id);die;
        //获取id为$perId 的当前权限数据
        $permission = Permission_::findOrFail($perId);
        //修改当前权限的权限名称
        $permission->update($arr);
        //修改关联 权限名称表和 权限说明表的 id信息 
        $permission->classify_belongsToMany()->sync($request->input('id'));
        //这是修改permissions_detail表的信息
        $details = new PermissionsDetails;
        //获取权限说明表的当前id的当前数据
        $is_permission = $details->where(array('permission_id' => $permission->id))->first();
        $is_permission->explain = $request->input('instructions');
        $is_permission->permission_id = $permission->id;
        if (empty($is_permission)) {
            $details->save();
        } else {
            $is_permission->update();
        }
        return response()->json(['code' => 20000, 'message' => '权限修改成功', 'data' => null]);
    }


    /**
     * Remove Permission from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        // if (!Gate::allows('users_manage')) {
        //     return abort(401);
        // }
        // echo 1;die;
        $id = $request->get('id');

        $permission = Permission_::findOrFail($id);
        $permission->delete();
        return response()->json(['code' => 20000, 'message' => '删除成功', 'data' => $id]);
    }

    /**
     * Delete all selected Permission at once.
     *
     * @param Request $request
     */
    public function massDestroy(Request $request)
    {
    }
    /**
     * Get a validator for an incoming change password request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
    }
}
